<template>
  <article>
  <el-row >
    <el-col :span="4"><div class="mygrid-content" ><span :style="{color:theme.txtcolor}">幻界游戏</span></div></el-col>
    <el-col :span="18"><div  class="mygrid-content" style="float: right;margin-right: 40px">
        <el-color-picker @change="colorChange" v-model="theme.bgcolor"></el-color-picker>
    </div></el-col>
    <el-col :span="2"><dropdown :name="user.name" :options="userMenu"/></el-col>
  </el-row>
  <el-dialog :visible="dialogVisible" :close-on-click-modal="false" :close-on-press-escape="false" title="修改密码"
             :modal-append-to-body="false"  :show-close="false">
    <el-form :model="form" :rules="rules" ref="form"  status-icon  label-position="left" style="width: 50%;margin-left: 25%" class="demo-form-inline"label-width="120px">
      <input id="id" type="hidden" v-model="user.id"/>
      <el-form-item label="原密码" prop="password">
        <el-input type="password"  v-model="form.password" auto-complete="off" placeholder="请输入原密码"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword">
        <el-input type="password"  v-model="form.newpassword" auto-complete="off" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newpassword2">
        <el-input type="password"  v-model="form.newpassword2" auto-complete="off" placeholder="请确认新密码"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
  </article>
</template>

<script>
    import store from '../../store'
    import dropdown from '@/components/dropdown.vue'
    import {mapState,mapGetters} from 'vuex';
    import * as api from "../../api/api";
    import {mapActions} from 'vuex';
    export default {
      components: {
        dropdown
      },
        data() {
          let validatePass = (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请输入密码'));
            } else{
              callback();
            }
          };
          let validatePass2 = (rule, value, callback) => {
            if (value === '') {
              callback(new Error('请再次输入密码'));
            } else if (value !== this.form.newpassword) {
              callback(new Error('两次输入密码不一致!'));
            } else {
              callback();
            }
          };
            return {userMenu:[
              // {
              //   index:1,
              //   name: '修改密码',
              //   events:this.updatePwd
              // },
                {
                index:2,
                name: '退出',
                events:this.mylogout
              }],
              dialogVisible: false,
              form: {
                id:'',
                newpassword: '',
                newpassword2:'',
                password: ''
              },
              rules: {
                password: [
                  { required: true, message: '请输入原密码', trigger: 'blur' }
                ],
                newpassword: [
                  { required: true, message: '请输入新密码', trigger: 'blur' }
                ],
                newpassword2: [
                  {required: true, validator: validatePass2, trigger: 'blur' }
                ]
              }
            };
        },
      computed:{
        ...mapState( {
          user: state =>{
            return JSON.parse(sessionStorage.getItem("user"))||state.user.user},
          theme: state => state.app.theme
        })
      },
      methods:{
        ...mapActions([
          'logout','updatePassword'
        ]),
        toLoginPage(){
          location.href = "http://120.92.15.43:8000/login";
          //this.$router.push({path: "/login"});
        },
        updatePwd:function () {
          this.dialogVisible = true;
        },
        submit(){
          this.$refs['form'].validate((valid) => {
            if (!valid) {
              console.log('error submit!!');
              return false;
            } else {
              this.form.id = document.getElementById("id").value;
              if(this.user.password===this.form.password){
                this.$message({
                  message: '原密码错误！',
                  type: 'warning'
                });
                return false;
              }
              this.updatePassword(this.user).then(() => {
                  this.$router.push({path: "/"});
                });
            }
          });
        },
        mylogout:function () {
          this.logout().then(this.toLoginPage);
        },
        colorChange(color){
          let theme = {};
          theme.bgcolor = color;
          let col = color.split("").slice(1).map((ch)=>{
            return ((parseInt(ch,16)+8)%16).toString(16);
          }).join("");
          theme.activeColor = "#"+col;
          theme.txtcolor="#000";
          theme.asideBgcolor="#f1f2f7";
          store.commit("setTheme",theme);
        },
        ...mapActions([
          'getThemeData'
        ]),
      }
    }

</script>

<style scoped lang="scss">
  .block{
    float: right;
    display: inline-block;
    width: 50%;
    box-sizing: border-box;
    .demonstration{
      display: block;
      font-size: 14px;
      margin-bottom: 20px;
    }
  }

  .mygrid-content {
    padding: 0px;
    margin: 0px;
    height: 70px;
    font-size: 300%;
  }
</style>
